<?php
require_once('../inc/func/isLogin.php');
require("../vendor/autoload.php");
require_once('../inc/data/dbfunc.php');
require_once('../inc/data/dbparam.php');
include('../inc/func/utils.php');
include('../inc/func/user.php');

use PhpOffice\PhpSpreadsheet\IOFactory;

// 读取文件
$spreadsheet = IOFactory::load($_FILES["file"]["tmp_name"]);

// 读取表中第一个表
$workSheet = $spreadsheet->getSheet(0);
// 获取行数最大值
$HighestDataRow = $workSheet->getHighestDataRow();
$successNum = 0;
$failNum = 0;
// 遍历读取表中每一行数据
for ($i = 3; $i < $HighestDataRow; $i++) {
    $stuID = $workSheet->getCell("B{$i}")->getValue();
    $stuName = $workSheet->getCell("C{$i}")->getValue();
    $salt = createSalt();
    $stuPasswd = encryptionPwd($salt, substr($stuID, -6)) ;

    // 插入数据
    $sql = "insert huangcz_stu (stu_id,stu_name,stu_pwd,salt,stu_class)
            values (?,?,?,?,2)";
    $conn = conn_mysql();
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('isss', $stuID, $stuName, $stuPasswd, $salt);
    $stmt->execute();
    $stmt->affected_rows == 1 ? $successNum++ : $failNum++;
}

echo <<< js
    <script>
        alert("导入成功人数: {$successNum},   失败人数: {$failNum}")
        location="./stuForm.php";
    </script>
js;
